package com.wjy.game;


import static java.lang.System.exit;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.Objects;

import javax.imageio.ImageIO;
import javax.swing.*;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by yaxuSong on 2015/10/21.
 */
@Slf4j
public class View {
    // 主界面
    public JFrame frame = new JFrame("五子棋");
    // 客户端名称
    public String clientName;
    // 房间名称列表
    public String[] roomName;

    public JLabel labelTitle = new JLabel("选择要加入的房间号:");
    public JButton okButton = new JButton("加入");
    public TextArea roomList = new TextArea();
    public JTextField choseTextField = new JTextField(10);

    // 左侧区域
    public JPanel panelLeft = new JPanel();
    // 右侧区域
    public JPanel panelRight = new JPanel();
    // 颜色
    public JPanel panelColor = new JPanel();
    // 发送聊天消息区域
    public JPanel controlPanel = new JPanel();
    // 棋手信息展示和信息提示
    public JPanel infoPanel = new JPanel();
    // 游戏控制区域
    public JPanel gameControlPanel = new JPanel();
    public JTextArea messageBox = new JTextArea();

    public JLabel jLabel = new JLabel("请选择棋子颜色:");

    // 颜色选择框
    public JComboBox comboBox = new JComboBox();
    public JPanel color = new JPanel();

    // 自定义圆角边框类
    class RoundedBorder implements javax.swing.border.Border {
        private int radius;

        RoundedBorder(int radius) {
            this.radius = radius;
        }

        @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(this.radius + 1, this.radius + 1, this.radius + 2, this.radius);
        }

        @Override
        public boolean isBorderOpaque() {
            return true;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
        }
    }
    // 聊天信息框
    public JTextField messageField = new JTextField();
    public JButton sendButton = new JButton("发送");
    // 角色信息
    public JLabel label = new JLabel("角色名称:");
    public JLabel roleNameLabel = new JLabel("");

    // 游戏控制按钮
    public JButton startButton = new JButton("开始游戏");
    public JButton createRoomButton = new JButton("创建房间");
    public JButton exitRoomButton = new JButton("退出房间");
    public JButton repentanceButton = new JButton("悔     棋");
    public JButton joinButton = new JButton("加入游戏");

    public View() {
        init();
        myEvent();
    }

    /**
     * 初始化界面
     */
    void init() {
        // 设置窗口图标
        try {
            Image icon = ImageIO.read(Objects.requireNonNull(getClass().getClassLoader().getResource("img.png")));
            frame.setIconImage(icon);
        } catch (IOException e) {
            log.error("无法加载图标文件: {}", e.getMessage());
        }

        clientName = JOptionPane.showInputDialog(frame, "请你的游戏角色起一个名字吧！", "提示", 2);

        if (clientName == null || clientName.equals("")) {
            if (clientName.equals("")) {
                JOptionPane.showMessageDialog(frame, "名字不能为空！");
            }
            exit(0);
        } else {
            roleNameLabel.setText(clientName);
        }

        frame.setResizable(false);
        panelLeft.setBounds(0, 0, 527, 565);
        panelRight.setBounds(527, 0, 271, 565);
        frame.setLocation(279, 139);
        frame.setSize(798, 565);
        frame.setLayout(null);
        panelLeft.setBackground(new Color(214, 214, 3));
        frame.getContentPane().add(panelRight);
        frame.getContentPane().add(panelLeft);
        panelLeft.setLayout(null);
        panelRight.setLayout(null);

        // 颜色设置区域
        panelColor.setBounds(0, 0, 279, 160);
        controlPanel.setBackground(Color.WHITE);
        controlPanel.setBounds(0, 280, 279, 80);
        panelRight.add(controlPanel);
        panelRight.add(panelColor);

        // 信息区域
//        infoPanel.setBackground(Color.BLUE);
        infoPanel.setBounds(0, 160, 279, 120);
        panelRight.add(infoPanel);

        // 游戏控制区
        gameControlPanel.setBackground(Color.RED);
        gameControlPanel.setBounds(0, 340, 279, 547 - 360);
        panelRight.add(gameControlPanel);

        frame.add(panelLeft);

        jLabel.setBounds(20, 10, 175, 20);
        comboBox.setBounds(20, 50, 219, 29);
        comboBox.insertItemAt("黑    色", 0);
        comboBox.insertItemAt("白    色", 1);
        comboBox.insertItemAt("红    色", 2);
        comboBox.insertItemAt("绿    色", 3);
        comboBox.insertItemAt("蓝    色", 4);
        comboBox.insertItemAt("粉    色", 5);
        comboBox.insertItemAt("黄    色", 6);
        comboBox.insertItemAt("橙    色", 7);
        comboBox.setSelectedIndex(0);
        panelColor.add(comboBox);
        panelColor.setLayout(null);
        color.setBounds(20, 110, 219, 29);
        color.setBackground(Color.black);
        panelColor.add(color);
        panelColor.add(jLabel);

        controlPanel.setLayout(null);
        sendButton.setBounds(210, 23, 60, 40);
        messageField.setBounds(0, 20, 209, 48);
        Font f = new Font("黑体", 1, 30);
        messageField.setFont(f);
        messageField.setBackground(new Color(245, 245, 245));
        messageField.setForeground(Color.BLACK);
        messageField.setBorder(new RoundedBorder(10));
        messageField.setMargin(new Insets(5, 10, 5, 10));
        controlPanel.add(messageField);
        controlPanel.add(sendButton);

        // 棋手信息部分
        infoPanel.setLayout(null);
        label.setBounds(20, 0, 88, 29);
        Font font = new Font("宋体", 5, 20);
        roleNameLabel.setFont(font);
        roleNameLabel.setBounds(60, 18, 190, 41);
        messageBox.setBounds(0, 55, 265, 65);
        infoPanel.add(messageBox);
        infoPanel.add(label);
        infoPanel.add(roleNameLabel);

        // 游戏控制部分
        gameControlPanel.setLayout(null);
        startButton.setBounds(88, 90, 88, 29);
        createRoomButton.setBounds(30, 55, 88, 29);
        joinButton.setBounds(148, 55, 88, 29);
        repentanceButton.setBounds(30, 125, 88, 29);
        exitRoomButton.setBounds(148, 125, 88, 29);
        exitRoomButton.setEnabled(false);
        repentanceButton.setEnabled(false);
        sendButton.setEnabled(false);
        startButton.setEnabled(false);

        gameControlPanel.add(startButton);
        gameControlPanel.add(createRoomButton);
        gameControlPanel.add(exitRoomButton);
        gameControlPanel.add(repentanceButton);
        gameControlPanel.add(joinButton);

        frame.setVisible(true);
    }

    void myEvent() {
        // 颜色选择框监听事件，同时变更颜色展示
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if ((String)e.getItem() == ("黑    色"))
                    color.setBackground(Color.black);
                if ((String)e.getItem() == ("白    色"))
                    color.setBackground(Color.white);
                if ((String)e.getItem() == ("红    色"))
                    color.setBackground(Color.red);
                if ((String)e.getItem() == ("绿    色"))
                    color.setBackground(Color.green);
                if ((String)e.getItem() == ("蓝    色"))
                    color.setBackground(Color.blue);
                if ((String)e.getItem() == ("粉    色"))
                    color.setBackground(Color.pink);
                if ((String)e.getItem() == ("黄    色"))
                    color.setBackground(Color.yellow);
                if ((String)e.getItem() == ("橙    色"))
                    color.setBackground(Color.orange);
            }
        });
    }
}
